<?php

class Admin_Controller extends Base_Controller {

	public $restful = true;

	public function get_index()
	{

		return Redirect::to('menu');
	}

	//get info on the current created menu
	public function get_menu()
	{

		$Menus = Menu::all();

		$selected = array();
		
		foreach ($Menus as $menu)
		{
			if($menu->action == NULL){
				$selected[$menu->controller] = array(
					'id' => $menu->menuid,
					'alias' => $menu->controlleralias,
					'footer' => ($menu->footer == 1)?'checked':'',
					'header' => ($menu->header == 1)?'checked':'',
					'arrangement' => $menu->arrangement ,
					'auth' => ($menu->auth == 1)?'checked':'',
					'admin' => ($menu->admin == 1)?'checked':''
					);
			}else{

				$selected[$menu->controller][$menu->action] = array(
					'id' => $menu->menuid,
					'alias' => $menu->actionalias,
					'footer' => ($menu->footer == 1)?'checked':'',
					'header' => ($menu->header == 1)?'checked':'',
					'arrangement' => $menu->arrangement ,
					'auth' => ($menu->auth == 1)?'checked':'',
					'admin' => ($menu->admin == 1)?'checked':''
					);
			}
		}


		$navigation = array();
		$controllers = glob('application/controllers/*.php',GLOB_NOESCAPE);
		$views = array();

		foreach ($controllers as $controllerName) {

			$ctrl = basename($controllerName,'.php');

			$views = glob('application/views/'.$ctrl.'/*.blade.php',GLOB_NOESCAPE);

			if(!in_array($ctrl, array('auth','base'))){
				if(!empty($views)){
					foreach ($views as $id => $viewsName) {
						$action = basename($viewsName,'.blade.php');
						// if($action != 'index'):
							$navigation[$ctrl][$id]['page'] = $action;
							$navigation[$ctrl][$id]['path'] = $viewsName;
						// endif;
					}
				}else{
					$navigation[$ctrl] = NULL;
				}
			}
		}


		$data['pageExist'] = Menu::pageAccesible();
		$data['selected'] = $selected;
		$data['navigation'] = $navigation;

		return View::make('admin.menu',$data);

	}

	public function post_menu()
	{

		$contents = Input::get();
		$ctrlArrangment = count($contents) + 1;

		foreach ($contents as $controller => $field) {

			if($field['alias'] != NULL && (isset($field['header']) || isset($field['footer']) || isset($field['id']))):

				if($field['id'] == 0):
					$menu = new Menu();
				else:
					$menu = Menu::find($field['id']);
				endif;
				
				$menu->controller = $controller;
				$menu->controllerAlias = $field['alias'];
				$menu->header = (isset($field['header']) && $field['header']=='on')?1:0;
				$menu->footer = (isset($field['footer']) && $field['footer']=='on')?1:0;
				$menu->auth = (isset($field['auth']) && $field['auth']=='on')?1:0;
				$menu->admin = (isset($field['admin']) && $field['admin']=='on')?1:0;
				$menu->arrangement = isset($field['arr'])?$field['arr']:$ctrlArrangment;
				$menu->save();

				$isAdmin = isset($field['admin'])?$field['admin']:0;

				unset($field['alias']);
				unset($field['header']);
				unset($field['footer']);
				unset($field['auth']);
				unset($field['arr']);
				unset($field['id']);
				unset($field['admin']);

				$actionArrangment = count($field) + 1;

				foreach ($field as $action => $actionVal) {

					if($actionVal['id'] == 0):
						$subMenu = new Menu();
					else:
						$subMenu = Menu::find($actionVal['id']);
					endif;

					if($actionVal['alias'] != '' && (isset($actionVal['header']) || isset($actionVal['footer']) || isset($actionVal['auth']) || isset($actionVal['id'])) ):

						$subMenu->controller = $controller;
						$subMenu->action = $action;
						$subMenu->actionAlias = $actionVal['alias'];
						$subMenu->header = (isset($actionVal['header']) && $actionVal['header']=='on')?1:0;
						$subMenu->footer = (isset($actionVal['footer']) && $actionVal['footer']=='on')?1:0;
						$subMenu->auth = (isset($actionVal['auth']) && $actionVal['auth']=='on')?1:0;
						$subMenu->admin = ($isAdmin=='on')?1:0;
						$subMenu->arrangement = isset($actionVal['arr'])?$actionVal['arr']:$actionArrangment;
						$subMenu->save();
					elseif($actionVal['alias'] == NULL && $actionVal['id'] != 0 ):
						$menu = Menu::find($actionVal['id'])->delete();
					endif;

					$actionArrangment++;
				}
			elseif($field['alias'] == NULL && $field['id'] != 0 ):
				$menu = Menu::find($field['id'])->delete();
			endif;

			$ctrlArrangment++;

		}


		return Redirect::to('admin/menu');

	}

	public function get_acl(){

		$data['rolelist'] = UserRole::all();
		$data['page'] = Menu::pageExist();//all();

		$data['acl'] = UserAcl::aclRegistered();

		return View::make('admin.acl',$data);
	}

	public function post_acl(){

		$input = Input::get();

		foreach($input as $role => $content){

			$role = UserRole::find($content['id']);

			$role->acl()->delete();

			unset($content['id']);

			foreach($content as $controler => $action){
				
				$mem = array_keys($action);

				foreach($mem as $mempage){
					$pages = array(
					    array('controller' => $controler,'action' => $mempage)
					);

					if(!empty($action)){
						$role->acl()->save($pages);
					}
				}
			}
		}

		return Redirect::to('admin/acl');
	}

	public function get_role(){

		$data['rolelist'] = $rolelist = UserRole::listRole();

		return View::make('admin.role',$data);
	}

	public function post_role(){

		$input = Input::get();

		if($input['roleid'] == NULL):
			$role = new UserRole;
		else:
			$role = UserRole::find($input['roleid']);
		endif;
        $role->role  = $input['role'];
        $role->save();

        return $role->roleid;

	}

	public function post_deleterole(){
		$input = Input::get();

		UserRole::find($input['id'])->acl()->delete();
		UserRole::find($input['id'])->delete();
	}

	public function get_navigation(){

		$data['navigation'] = $navigation = Nav::all();

		return View::make('admin.navigation',$data);
	}

}